package com.sxyjhh.controller;

import com.sxyjhh.common.DBConfig;
import com.sxyjhh.schedule.ScheduleTask;
import com.sxyjhh.service.data_exchange.DataExchangeService;
import com.sxyjhh.service.data_exchange.ImageService;
import com.sxyjhh.service.insert_person_option_certificate.InsertPersonOptionCertificate_InsertPersonOptionCertificatePort_Client;
import com.sxyjhh.utils.redis.RedisOperator;
import com.sxyjhh.utils.util.XaUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisCluster;

import java.sql.SQLException;
import java.util.Set;

/**
 * Created by Administrator on 2018/7/25.
 */
@RestController
@RequestMapping(value = "/service")
public class PushController {

    @Autowired
    private RedisOperator redisOperator;

    @Autowired
    DataExchangeService dataExchangeService;

    @Autowired
    ImageService imageService;

    @Autowired
    private JedisCluster jedisCluster;

    String COMPANY_SAFETY_CERTIFICATE_OUT = DBConfig.readPop("COMPANY.SAFETY.CERTIFICATE.OUT");

    String PERSON_SECURITY_CERTIFICATE_OUT = DBConfig.readPop("PERSON.SECURITY.CERTIFICATE.OUT");

    String PERSON_SPECIAL_CERTIFICATE_OUT = DBConfig.readPop("PERSON.SPECIAL.CERTIFICATE.OUT");

    String IMAGES_TABLE_NAME_OUT = DBConfig.readPop("IMAGES.TABLE.NAME.OUT");

    private static Logger log = Logger.getLogger(PushController.class);


    @RequestMapping(value = "/push", method = RequestMethod.GET)
    private void pushData(@RequestParam(value = "type", required = false, defaultValue = "ZJ") String type,
                          @RequestParam(value = "fType", required = false, defaultValue = "ag_ry") String fType){
       //安许证
        if("ZJ".equals(type)){
            int i = delRedisData(COMPANY_SAFETY_CERTIFICATE_OUT);
            log.info("===========删除缓存表===========");
            log.info(COMPANY_SAFETY_CERTIFICATE_OUT);
            log.info("缓存数据： "+i);
            dataExchangeService.startSynData();
        }
        //安管人员
        if("RY".equals(type)){
            int i = delRedisData(PERSON_SECURITY_CERTIFICATE_OUT);
            log.info("===========删除缓存表===========");
            log.info(PERSON_SECURITY_CERTIFICATE_OUT);
            log.info("缓存数据： "+i);
            dataExchangeService.startSynDataRy();
        }
        //安管人员 特种作业照片数据
        if("TP".equals(type)){
            imageService.startSynDataTP();
        }
        //特种作业人员
        if("TZ".equals(type)){
            int i = delRedisData(PERSON_SPECIAL_CERTIFICATE_OUT);
            log.info("===========删除缓存表===========");
            log.info(PERSON_SPECIAL_CERTIFICATE_OUT);
            log.info("缓存数据： "+i);
            dataExchangeService.startSynDataTz();
        }
    }

    @RequestMapping(value = "/pushDel", method = RequestMethod.GET)
    private void pushDataDel(@RequestParam(value = "type", required = false, defaultValue = "ZJ") String type){
        if("ZJ".equals(type)){
            int i = delRedisData(COMPANY_SAFETY_CERTIFICATE_OUT);
            log.info("===========删除缓存表===========");
            log.info(COMPANY_SAFETY_CERTIFICATE_OUT);
            log.info("缓存数据： "+i);
            dataExchangeService.startDelete("axz");
        }
        if("RY".equals(type)){
            int i = delRedisData(PERSON_SECURITY_CERTIFICATE_OUT);
            log.info("===========删除缓存表===========");
            log.info(PERSON_SECURITY_CERTIFICATE_OUT);
            log.info("缓存数据： "+i);
            dataExchangeService.startDelete("ag_ry");
        }
    }


    //删除信息
    public int delRedisData(String tabName) {
        //删除记录
        Set<String> set = redisOperator.keys(tabName + "*");
        int i = 0;
        if (!XaUtil.isEmpty(set)) {
            for (String redisKey : set) {
                i += jedisCluster.del(redisKey);
            }
        }

        return i;
    }
}
